package com.example.weektwo.dailysupplies.Three;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weektwo.AmountView;
import com.example.weektwo.Bean.ContentDatas;
import com.example.weektwo.Bean.InfoBean;
import com.example.weektwo.R;
import com.example.weektwo.dailysupplies.two.ChoiceAdapter;
import com.squareup.picasso.Picasso;

import java.io.Serializable;
import java.util.List;

public class ShopAdapter extends BaseAdapter {
    private static final String TAG = "ShopAdapter";
    Context context;
    List<InfoBean>mDatas;
    int buyCount;

    public ShopAdapter(Context context, List<InfoBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       ViewHolder holder=null;
        if (convertView==null) {
            convertView= LayoutInflater.from(context).inflate(R.layout.item_shop_lv,null);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        final InfoBean infoBean = mDatas.get(position);
        String title = infoBean.getTitle();//商品名称
        String kind = infoBean.getKind();//商品类别
        String pic = infoBean.getPic();//图片
        double price = infoBean.getPrice();//价格
        holder.name.setText(title);
        holder.kind.setText(kind);
        if (!TextUtils.isEmpty(pic)) {//判断图片是否为空
            Picasso.with(context).load(pic).into(holder.shopIv);
        }
        holder.price.setText("￥"+String.valueOf(price));
        holder.count.setAmountText(infoBean.getBuycount());
        holder.count.setGoods_storage(infoBean.getCount());  //设置最大值，库存
        holder.count.setOnAmountChangeListener(new AmountView.OnAmountChangeListener() {
            @Override
            public void onAmountChange(View view, int amount) {
                if (infoBean!=null) {
//                    重新设置购买数量
                    infoBean.setBuycount(amount);
                    ((ShoppingCartActivity)context).caculateTotalMoney();
                }
            }
        });

        holder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                取消购买，删除购买列表的数据
                ContentDatas.buyList.remove(infoBean);
                mDatas.remove(infoBean);   //将当前的数据减少
                notifyDataSetChanged();   //通知更新数据
//                当删除时，重新计算价格，并且重新设置给TextView
                ((ShoppingCartActivity)context).caculateTotalMoney();
            }
        });


        return convertView;
    }
    class ViewHolder{
        ImageView shopIv,delete;
        TextView name,kind,price;
        AmountView count;

        public ViewHolder(View view){
            shopIv=view.findViewById(R.id.item_shopcar_iv);
            name=view.findViewById(R.id.item_shopcar_name);
            kind=view.findViewById(R.id.choice_shopcar_kind);
            price=view.findViewById(R.id.item_shopcar_price);
            count=view.findViewById(R.id.shopcar_count);
            delete=view.findViewById(R.id.shopcar_delete);
        }
    }
}
